﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GUILayerWPF.FlightRoutingService;


/// <summary>
/// The GUILayerWPF namespace.
/// </summary>
namespace GUILayerWPF
{
    /// <summary>
    /// Interaction logic for CreateBooking.xaml
    /// </summary>
    public partial class CreateBooking : Window
    {

        //Variabler initialization
        private static FlightRouteServiceClient client;
        private Customer mainCustomer;
        private List<Customer> myCustomers;
        private Travel travel;
        private List<FlightInstance> fiList;


        public CreateBooking(Customer _customer)
        {
            fiList = new List<FlightInstance>();
            InitializeComponent();
            client = new FlightRouteServiceClient();
            ShowTimeComboBox();
            GetFromList();
            GetToList();
            GetPersonList();
            mainCustomer = _customer;
            myCustomers = new List<Customer>();
            myCustomers.Add(mainCustomer);
        }

        //Fill combobox
        private void GetFromList()
        {
            List<Airport> airports = client.SelectAllAirports().ToList();
            foreach (Airport airport in airports)
            {
                fromComboBox.Items.Add(airport.AirportName);
            }
        }
        //fill ComboBox
        private void GetToList()
        {
            List<Airport> airports = client.SelectAllAirports().ToList();
            foreach (Airport airport in airports)
            {
                toComboBox.Items.Add(airport.AirportName);
            }
        }
        //fill combobox
        private void GetPersonList()
        {
            for (int i = 1; i < 15; i++)
            {
                personComboBox.Items.Add(i);
            }
        }
        //Calculates the travel.
        private void CalculateTravel()
        {
            travel = new Travel();
            try
            {
                listBox.Items.Clear();
                travel.StartDestination =client.FindAirportByName(fromComboBox.SelectedItem.ToString());
                travel.EndDestination = client.FindAirportByName(toComboBox.SelectedItem.ToString());
                DateTime _flyDate = Calendar1.SelectedDate.Value;
                if(TimeHours != null)
                {
                    _flyDate = _flyDate.AddHours(System.Convert.ToDouble(TimeHours.Text)); 
                }
                else
                {
                    MessageBox.Show("DU har ikke angivet timer, gør venligst det");
                }
                 if(TimeMinuts != null)
                 {
                     _flyDate = _flyDate.AddMinutes(System.Convert.ToDouble(TimeMinuts.Text));
                    }
                    else
                    {
                        MessageBox.Show("DU har ikke angivet minutter, gør venligst det");
                    }
                 if (travel.StartDestination.AirportName.Equals(travel.EndDestination.AirportName))
                {
                    MessageBox.Show("De to rejsepunkter må ikke være ens.");
                }
                else
                {
                    if (personComboBox.SelectedIndex == -1)
                    {
                        MessageBox.Show("Vælg venligst det antal personer du ønsker på rejsen");
                    }
                    else
                    {
                        if (personComboBox.SelectedIndex + 1 > myCustomers.Count)
                        {
                            int count = personComboBox.SelectedIndex + 1;
                            PopUpCustomer popUpCustomer = new PopUpCustomer(mainCustomer, myCustomers, count);
                            popUpCustomer.Show();
                            MessageBox.Show("Tilføj venligst de andre personer til rejsen. Hvis personen allerede er oprettet som bruger, så søg blot på telefonnummeret. Hvis ikke, så opret vedkommende");
                        }
                        else
                        {
                            if (personComboBox.SelectedIndex + 1 < myCustomers.Count)
                            {
                                int count = personComboBox.SelectedIndex + 1;
                                PopUpCustomer popUpCustomer = new PopUpCustomer(mainCustomer, myCustomers, count);
                                popUpCustomer.Show();
                                MessageBox.Show("Antal personer på listen matcher ikke det ønskede antal. Fjern venligst de overflødige personer.");
                            }
                            else
                            {
                                if (quickRadioButton.IsChecked == true)
                                {
                                    fiList = client.GetQuickestRoute(travel.StartDestination, travel.EndDestination, _flyDate).ToList();
                                }
                                else
                                {
                                    if (cheapRadioButton.IsChecked == true)
                                    {
                                        try
                                        {
                                            fiList = client.GetCheapestRoute(travel.StartDestination, travel.EndDestination, _flyDate).ToList();
                                            listBox.Items.Add("Fra: " + travel.StartDestination.AirportName + ", Til: " + travel.EndDestination.AirportName);
                                        }
                                        catch(NullReferenceException)
                                        {
                                            MessageBox.Show("Ingen valid rute blev fundet");
                                        }
                                        catch(Exception)
                                        {
                                            MessageBox.Show("Ingen valid rute blev fundet. Prøv igen med et andet tidspunkt");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("Vælg venligst hvordan du vil prioritere din rejse.");
                                    }//end else
                                }//end else
                            }//end else
                        }//end else
                    }//end else
                        
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Vælg venligst dine rejsepunkter.");
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Vælg venligst en dato for afgangsrejse.");
            }
        }

        //button "søg" click
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            listBox.Items.Clear();
            CalculateTravel();
        }
        //button return click
        private void returnBtn_Click(object sender, RoutedEventArgs e)
        {

            createCustomer customerUI = new createCustomer();
            customerUI.Show();
            this.Close();
        }
        //button "vælg" click
        private void ChooseSeatBtn_Click_1(object sender, RoutedEventArgs e)
        {
            if (listBox.SelectedIndex == -1)
            {
                MessageBox.Show("Vælg venligst en rejse, tak");
            }
            else
            {
                travel.Persons = myCustomers.ToArray();
                int index = 0;
                List<SeatBooking> bookings = new List<SeatBooking>();
                while (index < fiList.Count)
                {
                    SeatBooking booking = new SeatBooking();
                    //Timeout exception
                    FlightInstance flightinstance = client.FindFlightInstanceById(fiList[index].FlightInstanceId);
                    booking.FlightInstance = flightinstance;
                    bookings.Add(booking);
                    index++;
                }
                travel.SeatBooks = bookings.ToArray();
                try
                {
                    FlightInstance flightInstance = fiList[listBox.SelectedIndex];
                    SeatBookingGUI SeatBookingUI = new SeatBookingGUI(travel, mainCustomer);
                    SeatBookingUI.Show();
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("Der er ikke valgt nogen rejse, prøv igen");
                }
            }

        }
        //Fill ComboBox
        private void ShowTimeComboBox()
        {
            int index = 0;
            while(index < 60)
            {
                if (index < 10)
                {
                    TimeHours.Items.Add("0" + index);
                    TimeMinuts.Items.Add("0" + index);
                }
                else
                {
                    if(index < 24)
                    {
                        TimeHours.Items.Add(index);
                    }
                    TimeMinuts.Items.Add(index);
                    
                }
                
                index++;
            }
        }
    }
}
